import { Prop, Provide, Emit, Model } from 'vue-property-decorator';
import { Subject, Subscription } from 'rxjs';
import { UIActionTool,Util,ViewTool } from '@/utils';
import { Watch, GridControlBase } from '@/studio-core';
import EMPlanTemplService from '@/service/emplan-templ/emplan-templ-service';
import Main2Service from './main2-grid-service';
import EMPlanTemplUIService from '@/uiservice/emplan-templ/emplan-templ-ui-service';
import { FormItemModel } from '@/model/form-detail';


/**
 * grid部件基类
 *
 * @export
 * @class GridControlBase
 * @extends {Main2GridBase}
 */
export class Main2GridBase extends GridControlBase {

    /**
     * 获取部件类型
     *
     * @protected
     * @type {string}
     * @memberof Main2GridBase
     */
    protected controlType: string = 'GRID';

    /**
     * 建构部件服务对象
     *
     * @type {Main2Service}
     * @memberof Main2GridBase
     */
    public service: Main2Service = new Main2Service({ $store: this.$store });

    /**
     * 实体服务对象
     *
     * @type {EMPlanTemplService}
     * @memberof Main2GridBase
     */
    public appEntityService: EMPlanTemplService = new EMPlanTemplService({ $store: this.$store });

    /**
     * 应用实体名称
     *
     * @protected
     * @type {string}
     * @memberof Main2GridBase
     */
    protected appDeName: string = 'emplantempl';

    /**
     * 应用实体中文名称
     *
     * @protected
     * @type {string}
     * @memberof Main2GridBase
     */
    protected appDeLogicName: string = '计划模板';

    /**
     * 界面UI服务对象
     *
     * @type {EMPlanTemplUIService}
     * @memberof Main2Base
     */  
    public appUIService:EMPlanTemplUIService = new EMPlanTemplUIService(this.$store);


    /**
     * 界面行为模型
     *
     * @type {*}
     * @memberof Main2Base
     */  
    public ActionModel: any = {
    };

    /**
     * 主信息表格列
     *
     * @type {string}
     * @memberof Main2Base
     */  
    public majorInfoColName:string = "emplantemplname";

    /**
     * 列主键属性名称
     *
     * @type {string}
     * @memberof Main2GridBase
     */
    public columnKeyName: string = "emplantemplid";

    /**
     * 本地缓存标识
     *
     * @protected
     * @type {string}
     * @memberof Main2Base
     */
    protected localStorageTag: string = 'emplantempl_main2_grid';

    /**
     * 所有列成员
     *
     * @type {any[]}
     * @memberof Main2GridBase
     */
    public allColumns: any[] = [
        {
            name: 'emplantemplid',
            label: '计划模板编号',
            langtag: 'entities.emplantempl.main2_grid.columns.emplantemplid',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'emplantemplname',
            label: '计划模板名称',
            langtag: 'entities.emplantempl.main2_grid.columns.emplantemplname',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'plantype',
            label: '计划类型',
            langtag: 'entities.emplantempl.main2_grid.columns.plantype',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'planstate',
            label: '计划状态',
            langtag: 'entities.emplantempl.main2_grid.columns.planstate',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'rempname',
            label: '责任人',
            langtag: 'entities.emplantempl.main2_grid.columns.rempname',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'rdeptname',
            label: '责任部门',
            langtag: 'entities.emplantempl.main2_grid.columns.rdeptname',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'rteamname',
            label: '责任班组',
            langtag: 'entities.emplantempl.main2_grid.columns.rteamname',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'rservicename',
            label: '服务商',
            langtag: 'entities.emplantempl.main2_grid.columns.rservicename',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'recvpersonname',
            label: '接收人',
            langtag: 'entities.emplantempl.main2_grid.columns.recvpersonname',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'mpersonname',
            label: '制定人',
            langtag: 'entities.emplantempl.main2_grid.columns.mpersonname',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'mdate',
            label: '制定时间',
            langtag: 'entities.emplantempl.main2_grid.columns.mdate',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'activelengths',
            label: '持续时间(H)',
            langtag: 'entities.emplantempl.main2_grid.columns.activelengths',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'eqstoplength',
            label: '停运时间(分)',
            langtag: 'entities.emplantempl.main2_grid.columns.eqstoplength',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'plandesc',
            label: '计划内容',
            langtag: 'entities.emplantempl.main2_grid.columns.plandesc',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'mtflag',
            label: '多任务?',
            langtag: 'entities.emplantempl.main2_grid.columns.mtflag',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'prefee',
            label: '预算(￥)',
            langtag: 'entities.emplantempl.main2_grid.columns.prefee',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
        {
            name: 'archive',
            label: '归档',
            langtag: 'entities.emplantempl.main2_grid.columns.archive',
            show: true,
            unit: 'PX',
            isEnableRowEdit: false,
            enableCond: 3 ,
        },
    ]

    /**
     * 获取表格行模型
     *
     * @type {*}
     * @memberof Main2GridBase
     */
    public getGridRowModel(){
        return {
          srfkey: new FormItemModel(),
        }
    }

    /**
     * 属性值规则
     *
     * @type {*}
     * @memberof Main2GridBase
     */
    public rules: any = {
        srfkey: [
            { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '计划模板编号 值不能为空', trigger: 'change' },
            { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '计划模板编号 值不能为空', trigger: 'blur' },
        ],
    }

    /**
     * 属性值规则
     *
     * @type {*}
     * @memberof Main2Base
     */
    public deRules:any = {
    };

    /**
     * 获取对应列class
     *
     * @type {*}
     * @memberof Main2Base
     */
    public hasRowEdit: any = {
        'emplantemplid':false,
        'emplantemplname':false,
        'plantype':false,
        'planstate':false,
        'rempname':false,
        'rdeptname':false,
        'rteamname':false,
        'rservicename':false,
        'recvpersonname':false,
        'mpersonname':false,
        'mdate':false,
        'activelengths':false,
        'eqstoplength':false,
        'plandesc':false,
        'mtflag':false,
        'prefee':false,
        'archive':false,
    };

    /**
     * 获取对应列class
     *
     * @param {*} $args row 行数据，column 列数据，rowIndex 行索引，列索引
     * @returns {void}
     * @memberof Main2Base
     */
    public getCellClassName(args: {row: any, column: any, rowIndex: number, columnIndex: number}): any {
        return ( this.hasRowEdit[args.column.property] && this.actualIsOpenEdit ) ? "edit-cell" : "info-cell";
    }


    /**
     * 导出数据格式化
     *
     * @param {*} filterVal
     * @param {*} jsonData
     * @param {any[]} [codelistColumns=[]]
     * @returns {Promise<any>}
     * @memberof Main2GridBase
     */
    public async formatExcelData(filterVal: any, jsonData: any, codelistColumns?: any[]): Promise<any> {
        return super.formatExcelData(filterVal, jsonData, [
            {
                name: 'plantype',
                srfkey: 'EMPLANTYPE',
                codelistType : 'STATIC',
                renderMode: 'other',
                textSeparator: '、',
                valueSeparator: ',',
            },
            {
                name: 'planstate',
                srfkey: 'EMPLANSTATE',
                codelistType : 'STATIC',
                renderMode: 'other',
                textSeparator: '、',
                valueSeparator: ',',
            },
            {
                name: 'mtflag',
                srfkey: 'YesNo',
                codelistType : 'STATIC',
                renderMode: 'other',
                textSeparator: '、',
                valueSeparator: ',',
            },
            {
                name: 'archive',
                srfkey: 'EMARCHIVE',
                codelistType : 'STATIC',
                textSeparator: '、',
                renderMode: 'string',
                valueSeparator: ";",
            },
        ]);
    }


    /**
     * 更新默认值
     * @param {*}  row 行数据
     * @memberof Main2Base
     */
    public updateDefault(row: any){                    
    }

    /**
     * 计算数据对象类型的默认值
     * @param {string}  action 行为
     * @param {string}  param 默认值参数
     * @param {*}  data 当前行数据
     * @memberof Main2Base
     */
    public computeDefaultValueWithParam(action:string,param:string,data:any){
        if(Object.is(action,"UPDATE")){
            const nativeData:any = this.service.getCopynativeData();
            if(nativeData && (nativeData instanceof Array) && nativeData.length >0){
                let targetData:any = nativeData.find((item:any) =>{
                    return item.emplantemplid === data.srfkey;
                })
                if(targetData){
                    return targetData[param]?targetData[param]:null;
                }else{
                    return null;
                }
            }else{
                return null;
            }
        }else{
           return this.service.getRemoteCopyData()[param]?this.service.getRemoteCopyData()[param]:null;
        }
    }


}